<template>
	<view class="container">
		<!-- 小程序 -->
<!-- 		<live-player
		  :src="pull_stream"
		  autoplay
		  @statechange="statechange"
		  @error="error"
		  style="width: 300px; height: 225px;"
		/> -->
		<!-- 安卓 -->
		<video :src="pull_stream"  is-live  ></video>
		<!-- 显示视频 -->
		<!-- <video src="/static/video.mp4" controls></video> -->
		<!-- <video :src="videourl" controls></video> -->
		<!-- 显示音频 -->
		<!-- <audio src="/static/audio.wav"></audio> -->
<!-- 		<view>
			<live-pusher id='livePusher' ref="livePusher" class="livePusher" :url="videourl" mode="SD" :muted="true"
				:enable-camera="true" :auto-focus="true" :beauty="1" whiteness="2" aspect="9:16"
				@statechange="statechange" @netstatus="netstatus" @error="error"></live-pusher>
			<button class="btn" @click="start">开始推流</button>
			<button class="btn" @click="pause">暂停推流</button>
			<button class="btn" @click="resume">resume</button>
			<button class="btn" @click="stop">停止推流</button>
			<button class="btn" @click="snapshot">快照</button>
			<button class="btn" @click="startPreview">开启摄像头预览</button>
			<button class="btn" @click="stopPreview">关闭摄像头预览</button>
			<button class="btn" @click="switchCamera">切换摄像头</button>
		</view> -->
	</view>
</template>
<script>
	export default {
		data() {
			return {
				videourl: "http://pullvideo.dtsdwh.cn/live/test2.flv?txSecret=0050673d915b4e9412f5ef68dbf2c11d&txTime=65E0018A",
				pull_stream:"webrtc://pullvideo.dtsdwh.cn/live/test2?txSecret=0ebfb7db1ff0a968147ba003f86cefc0&txTime=65E0054D",
				// pull_stream:"rtmp://pullvideo.dtsdwh.cn/live/test2?txSecret=0ebfb7db1ff0a968147ba003f86cefc0&txTime=65E0054D",
				// videourl: "http://192.168.1.199:8102/static/video/89CCD423870BC51C3771197B6DECCC66.mp4",
				pushUrl: 'rtmp://196244.push.tlivecloud.com/live/test?txSecret=6ad2aed19e64df229c29b9f2117bc465&txTime=65DEB3CA', // 推流服务器地址
				streamKey: '' // 推流密钥
			}
		},
		onReady() {
			// 注意：需要在onReady中 或 onLoad 延时
			this.context = uni.createLivePusherContext("livePusher", this);
		},
		mounted() {
			// this.startStreaming()
		},
		methods: {
			startStreaming() {
				const options = {
					// url: this.pushUrl + '/stream', // 设置推流URL
					url: this.pushUrl, // 设置推流URL
					key: this.streamKey, // 设置推流密钥
					videoSource: document.querySelector('video'), // 获取视频元素
					audioSource: document.querySelector('audio') // 获取音频元素
				}

				new Pusher(options).init().then(() => {
					console.log('推流已经开始！')
				}).catch((error) => {
					console.error('推流失败：', error)
				})
			},
			statechange(e) {
				console.log("statechange:" + JSON.stringify(e));
			},
			netstatus(e) {
				console.log("netstatus:" + JSON.stringify(e));
			},
			error(e) {
				console.log("error:" + JSON.stringify(e));
			},
			start: function() {
				this.context.start({
					success: (a) => {
						console.log("livePusher.start:" + JSON.stringify(a));
					}
				});
			},
			close: function() {
				this.context.close({
					success: (a) => {
						console.log("livePusher.close:" + JSON.stringify(a));
					}
				});
			},
			snapshot: function() {
				this.context.snapshot({
					success: (e) => {
						console.log(JSON.stringify(e));
					}
				});
			},
			resume: function() {
				this.context.resume({
					success: (a) => {
						console.log("livePusher.resume:" + JSON.stringify(a));
					}
				});
			},
			pause: function() {
				this.context.pause({
					success: (a) => {
						console.log("livePusher.pause:" + JSON.stringify(a));
					}
				});
			},
			stop: function() {
				this.context.stop({
					success: (a) => {
						console.log(JSON.stringify(a));
					}
				});
			},
			switchCamera: function() {
				this.context.switchCamera({
					success: (a) => {
						console.log("livePusher.switchCamera:" + JSON.stringify(a));
					}
				});
			},
			startPreview: function() {
				this.context.startPreview({
					success: (a) => {
						console.log("livePusher.startPreview:" + JSON.stringify(a));
					}
				});
			},
			stopPreview: function() {
				this.context.stopPreview({
					success: (a) => {
						console.log("livePusher.stopPreview:" + JSON.stringify(a));
					}
				});
			}
		}
	}
</script>

<style>

</style>